//
//  RootBaseController.m
//  JxCarios
//
//  Created by xcq on 15/1/20.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "RootBaseController.h"
#import "HomeViewController.h"
#import "HomeBaseViewController.h"
#import "RecreationViewController.h"
#import "UserZoneViewController.h"
#import "DayGuaViewController.h"
#import "CLUIBaseNavigationController.h"
#import "CarAroundViewController.h"
#import "MJTabbarItem.h"
#import "CustomTabbarView.h"
static BOOL popHidden = NO;

@implementation RootBaseController{

    CustomTabbarView *_btn;
    
}

- (void)timeZoneJudge {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // something
        [Common setBaiDay];
    });
}

+(instancetype)getInstance{
    RootBaseController *roorBar = [[RootBaseController alloc] init];
    
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    HomeBaseViewController *home = [HomeBaseViewController getInstance];
    UIImage *homeimage = [[UIImage imageNamed:@"shouye_chu"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    home.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:homeimage tag:101];
     [roorBar addChildViewController:home];
 
    
    HomeBaseViewController *home_temp = [HomeBaseViewController getInstance];
     home_temp.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[Common resizeImage:@"1_test"] tag:101];
    [roorBar addChildViewController:home_temp];
 
    
    DayGuaViewController *day = [story instantiateViewControllerWithIdentifier:@"DayGuaView"];
    UIImage *dayGuaImage = [[UIImage imageNamed:@"meiri_chu"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

    day.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"每日一刮" image:dayGuaImage tag:101];

     [roorBar addChildViewController:day];
    
    UserZoneViewController *user = [story instantiateViewControllerWithIdentifier:@"UserZoneView"];
    UIImage *userZoneImage = [[UIImage imageNamed:@"geren_chu"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

    user.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"个人中心" image:userZoneImage tag:101];
    [user setTitle:@"个人中心"];
    CLUIBaseNavigationController *nav = [[CLUIBaseNavigationController alloc] initWithRootViewController:user];
 
    [roorBar addButton:roorBar];
    [roorBar addChildViewController:nav];
    [roorBar timeZoneJudge];
    roorBar.tabBar.tintColor = BUTTUN_COLOR;
    return roorBar;
    
}

- (void)addButton:(RootBaseController *)root{
//    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btn.tag = 400;
//    [_btn setBackgroundColor:BUTTUN_COLOR];
//    [_btn setTitle:@"车服务" forState:UIControlStateNormal];
//    [_btn setFont:[UIFont systemFontOfSize:10.0f]];
//    [_btn setImage:[UIImage imageNamed:@"chefuwu"] forState:UIControlStateNormal];
//    [_btn addTarget:root action:@selector(carAround:) forControlEvents:UIControlEventTouchUpInside];
//    if([RextExtends getScreenFrame].size.width == 320){
//        [_btn setFrame:CGRectMake(85, root.tabBar.frame.origin.y  , 65 , root.tabBar.frame.size.height)];
//    }else {
//       [_btn setFrame:CGRectMake((self.tabBar.frame.size.width / 4 ) + 13, root.tabBar.frame.origin.y  , 65 , root.tabBar.frame.size.height)];
//    }
    //[_btn setImageEdgeInsets:UIEdgeInsetsMake(-7, _btn.frame.size.width / 2 - (_btn.imageView.frame.size.width) + 3, 0, 0  )];
 
//    _btn.imageView.frame = CGRectMake(_btn.frame.size.width / 2 - (_btn.imageView.frame.size.width /2 ), 16, 13.3, 17);
//    [_btn setTitleEdgeInsets:UIEdgeInsetsMake(35,  (_btn.imageEdgeInsets.left - 30), 0, 0)];
//    
    
    _btn = [[[NSBundle mainBundle] loadNibNamed:@"CustomTabbarView" owner:self options:nil] firstObject];
    _btn.tag = 400;
    if([RextExtends getScreenFrame].size.width == 320){
        [_btn setFrame:CGRectMake(85, root.tabBar.frame.origin.y  , 65 , root.tabBar.frame.size.height)];
    }else if ([RextExtends getScreenFrame].size.width == 375){
        [_btn setFrame:CGRectMake((self.tabBar.frame.size.width / 4 ) , root.tabBar.frame.origin.y  , 70 , root.tabBar.frame.size.height)];
    }else if ([RextExtends getScreenFrame].size.width == 414){
        [_btn setFrame:CGRectMake((self.tabBar.frame.size.width / 4 ) , root.tabBar.frame.origin.y  , 80 , root.tabBar.frame.size.height)];
    }else {
        [_btn setFrame:CGRectMake(85, root.tabBar.frame.origin.y  , 65 , root.tabBar.frame.size.height)];
    }
    _btn.backgroundColor = root.tabBar.backgroundColor;
    [_btn.imageView.image imageWithRenderingMode:UIImageRenderingModeAutomatic];
    [_btn.btn addTarget:root action:@selector(carAround:) forControlEvents:UIControlEventTouchUpInside];
    [root.view addSubview:_btn];
    
}


- (void)touchMaskView{
    [self popViewHidden:_btn];
}

- (void) initPopView{
    NSLog(@"[UIScreen mainScreen].bounds.size.width:%f",[UIScreen mainScreen].bounds.size.width);
    _popView = [[UIView alloc] initWithFrame:CGRectMake(0, _btn.frame.origin.y - _btn.frame.size.height , [RextExtends getScreenFrame].size.width, _btn.frame.size.height * 2)];
    _popView.tag = 511;
    _popTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [RextExtends getScreenFrame].size.width, _btn.frame.size.height)];
    _popTopView.backgroundColor = UICOLOR_MAKE_RGB(212, 212, 212);
    CGFloat btnWidth = 66;
    CGFloat spacing = 10;
    if ([UIScreen mainScreen].bounds.size.width == 375) { //iphone6
        btnWidth = 75;
        spacing = 15;
    }
    if([UIScreen mainScreen].bounds.size.width == 414){
        btnWidth = 80;
        spacing = 18;
    }
    
    NSArray *array = @[@"保养",@"洗车",@"维修",@"美食"];
    for (int i = 0; i < 4; i++) {
        MJTabbarItem *btn = [[MJTabbarItem alloc] initWithFrame:CGRectMake(spacing + i * (spacing + btnWidth), 5, btnWidth, 40)];
        if (i <= 2) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.origin.x + btn.frame.size.width + spacing/2, btn.frame.origin.y, 1, 40)];
            view.layer.contents = (id)[UIImage imageNamed:@"fengexian"].CGImage;
            [_popTopView addSubview:view];
        }
        [btn setTitle:array[i] icon:[NSString stringWithFormat:@"shouye_%d",i]];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(carAroundSeleted:) forControlEvents:UIControlEventTouchUpInside];
         btn.tag = 200 + i;
        [btn setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
        [_popTopView addSubview:btn];
    }
    _popBtmView = [[UIView alloc] initWithFrame:CGRectMake(0, _btn.frame.size.height, [RextExtends getScreenFrame].size.width, _btn.frame.size.height)];
    MJTabbarItem *item = [[MJTabbarItem alloc] initWithFrame:CGRectMake((self
                                                                         .tabBar.frame.size.width / 2) - (65 / 2), 5,_btn.frame.size.width,_popBtmView.frame.size.height)];
    [item setTitle:@"返回" icon:@"fanhui_che"];
    item.titleLabel.textColor = [UIColor blackColor];
    [item setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [item addTarget:self action:@selector(carAroundSeleted:) forControlEvents:UIControlEventTouchUpInside];
    item.tag = 204;
    [_popBtmView addSubview:item];
    _popBtmView.backgroundColor = UICOLOR_MAKE_RGB(81, 81, 81);
    [_popView addSubview:_popTopView];
    [_popView addSubview:_popBtmView];
    _popView.alpha = 0.1;
    [self.view addSubview:_popView];
}

/*
 保养（upkeep）
 洗车（jzxc）
 维修（weixiu）
 */
- (void)carAroundSeleted:(UIButton *)sender{
    CarServiceTableViewController *carService;
    switch (sender.tag) {
        case 200:
        {
            NSLog(@"保养");
           carService = [[CarServiceTableViewController alloc] init:@"upkeep" andIsSendMessegeIsCategory:NO];
        
        }
            break;
        case 201:{
            NSLog(@"洗车");
            carService = [[CarServiceTableViewController alloc] init:@"jzxc" andIsSendMessegeIsCategory:NO];

        }
            break;
        case 202:{
            NSLog(@"维修");
            carService = [[CarServiceTableViewController alloc] init:@"weixiu" andIsSendMessegeIsCategory:NO];

        }
            break;
        case 203:{
            NSLog(@"美食");
            UIStoryboard *stroy = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            WXCarAroundViewController *carAround = [stroy instantiateViewControllerWithIdentifier:@"carAround"];         carAround.keyWord = @"美食";
            carAround.Navtitle = @"车周边";
            carAround.keyString1 = @"root";
             CLUIBaseNavigationController * nav = [[CLUIBaseNavigationController alloc] initWithRootViewController:carAround];
            [self presentViewController:nav animated:YES completion:^{
                
            }];
        }
            break;
        case 204:{
            
        }
            break;
        default:
            break;
    }
    if (sender.tag != 204) {
        CLUIBaseNavigationController * nav = [[CLUIBaseNavigationController alloc] initWithRootViewController:carService];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }
    [self popViewHidden:_btn];
   
}

- (void)carAround:(UIButton *)sender{
    if (!_popView) {
        _maskView = [[UIView alloc] initWithFrame:self.view.bounds];
        _maskView.backgroundColor = [UIColor grayColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchMaskView)];
        [_maskView addGestureRecognizer:tap];
        _maskView.alpha = 0.4;
        [self initPopView];
    
        [self.view addSubview:_maskView];
        [self.view insertSubview:_maskView belowSubview:_popView];
        _popView.hidden = YES;
        _maskView.hidden = YES;
    }
    [self popViewHidden:(CustomTabbarView *)sender.superview];
}

- (void)popViewHidden:(CustomTabbarView *)sender{
    
    if (popHidden) {
        [UIView animateWithDuration:0.5f animations:^{
            _maskView.hidden = YES;
            [_popView setFrame:CGRectMake(0, [RextExtends getScreenFrame].size.height, [RextExtends getScreenFrame].size.width, sender.frame.size.height * 2)];
            _popView.alpha = 0.1f;
            popHidden = NO;
        } completion:^(BOOL finished) {
            _popView.hidden = YES;
            _popBtmView.hidden = YES;
            _popTopView.hidden = YES;
        }];
    }else{
        _popBtmView.hidden = NO;
        _popView.hidden = NO;
        _popTopView.hidden = NO;
        [_popView setBackgroundColor:[UIColor lightGrayColor]];
        [UIView animateWithDuration:0.5f animations:^{
            _maskView.hidden = NO;
        
            _popView.alpha = 1.0f;
            [_popView setFrame:CGRectMake(0, sender.frame.origin.y - sender.frame.size.height , [RextExtends getScreenFrame].size.width, sender.frame.size.height * 2)];
            _popView.hidden = NO;
            popHidden = YES;
        }];
    }

}



#pragma mark 选中某个控制器 YES代表重复点击了
- (BOOL)setSelected:(int)index
{
     if (index < 0 || index>=self.childViewControllers.count) return NO;
    // 1.取出新控制器
    UINavigationController *new = [self.childViewControllers objectAtIndex:index];
    if (_selectedController == new) return YES;
    
    // 2.移除旧控制器的view
    [_selectedController.view removeFromSuperview];
    _selectedController = new;
    
    
    return NO;
}


@end
